/*!
 * \file t___es_get_action.c
 *
 * \brief __es_get_action 单体测试
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 * ct管理用共通函数
 *
 * \version 4.0
 * \author geyh
 * \date 2016/08/29
 *
 */

#include <CUnit/Basic.h>
#include <stdlib.h>


#include "frcomm.h"
#include "frmem.h"
#include "frbyte_buffer.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "../../../src/module/es/config.h"
#include <es/es_core.h>
#include <frreport_comm.h>

#include "apmtest_cunit.h"
#include "apmtest_comm.h"
#include "../../../src/module/es/es_core.h"
#include "../test_es_comm.h"

static void __list_report_node_free(Frlist_node *p_st_node)
{
    Frreport_data *p_st_report_data=NULL;
    if(p_st_node==NULL || p_st_node->data == NULL )
        return;

    p_st_report_data = (Frreport_data*)FRLIST_GET_NODE_DATA(p_st_node);
    if(p_st_report_data)
    {
        frreport_data_free(&p_st_report_data);
    }

    return;
}


extern es_private* __es_private_create(void);
extern int __es_response(Frbyte_Packet *p_st_packet, es_private *p_st_pritate, Frreport_body_data *p_st_report_body, Frhash_header *p_st_hash_report);

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void case1(void)
{
    Frbyte_Packet *p_st_data = NULL;
    es_private *p_st_private = NULL;
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_report_body = NULL;
    Frhash_header *p_st_hash_report = NULL;
    int i_ret = 0;

    p_st_data = bytebuff_creat();

    bytebuff_add(p_st_data, (char *)"\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x0f\xb8\x9b\x00", 15);

    p_st_private = __es_private_create();

    test_es_create_ct(&p_st_conn, TEST_ES_SRC_IP, TEST_ES_DST_IP, TEST_ES_SRC_PORT, TEST_ES_DST_PORT);
    p_st_report_body = frreport_body_new_l7(p_st_conn);

    p_st_hash_report = frhash2_create2(2);

    i_ret = __es_response(p_st_data, p_st_private, p_st_report_body, p_st_hash_report);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);

    frhash2_free2(p_st_hash_report, __list_report_node_free);
    bytebuff_free(&p_st_data);
    frreport_body_free(&p_st_report_body);
    test_es_destroy_ct(&p_st_conn);
    es_session_destroy(p_st_private);

    return;
}

static void case2(void)
{
    Frbyte_Packet *p_st_data = NULL;
    es_private *p_st_private = NULL;
    Frreport_body_data *p_st_report_body = NULL;
    Frhash_header *p_st_hash_report = NULL;
    int i_ret = 0;

    i_ret = __es_response(p_st_data, p_st_private, p_st_report_body, p_st_hash_report);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    return;
}


int add_suite___es_response(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __es_response()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL
        || CU_add_test(pSuite, "case2", case2) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
